home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / video / security / security.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  11.7 KB  |  433 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <Vk/VkApp.h>
  18. #include <Vk/VkMenuBar.h>
  19. #include <Vk/VkSubMenu.h>
  20. #include <Xm/Form.h>
  21. #include <Xm/RowColumn.h>
  22. #include <stdlib.h>
  23. #include <limits.h>
  24. #include <libgen.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include "security.h"
  28. #include <sys/signal.h>
  29. #include <siginfo.h>
  30. #define MAX(a,b) (((a)>(b))?(a):(b))
  31.  
  32. extern VkApp *securityApp;
  33. extern int devicenum;
  34. extern char *flipImage(char *,int,int);
  35.  
  36. #ifdef SAVER
  37. extern  "C" void sigSaver(Widget, int);
  38. #endif
  39.  
  40. #ifdef NOSHIP
  41. #include <MovieController.h>
  42. #endif
  43.  
  44. #ifdef DEBUG
  45. #define dprintf printf
  46. #else
  47. #define dprintf 0&& 
  48. #endif
  49.  
  50. SecurityWin::~SecurityWin()
  51. {
  52.   delete _pref;
  53.   delete _startStop;
  54.   delete _compress;
  55.   delete _visual;
  56.   delete _checkBox;
  57.   delete _fileMenu;
  58.   delete _mainMenu;
  59. }
  60.  
  61. const char *SecurityWin::className() { return "SecurityWin";}
  62.  
  63. void SecurityWin::quitCallback( Widget, XtPointer, XtPointer)
  64. {
  65.   theApplication->quitYourself();
  66. }
  67.  
  68. SecurityWin::SecurityWin(const char *name) : VkWindow(name)
  69. {
  70.   Arg args[10];
  71.   int n;
  72.   _pref = new Pref("preferences");
  73.   Widget _form = XmCreateForm(mainWindowWidget(),"form",NULL,0);
  74.   _compress = new  CompressPanel("cmenu",_form);
  75.   _compress->setFileName("security.mv");
  76.   _compress->setFrameRate(-1.0);
  77.   n = 0;
  78.   XtSetArg(args[n],XmNtopAttachment, XmATTACH_FORM);n++;
  79.   XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
  80.   XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
  81.   XtSetValues(_compress->baseWidget(), args, n);
  82.   _compress->show();
  83.   _checkBox = new VkCheckBox("misc",_form);
  84.   _checkBox->addItem("Audio",TRUE);
  85.   _checkBox->addItem("Monitor",FALSE);
  86.   _checkBox->addItem("Saver",TRUE);
  87. #ifdef DPS
  88.   _checkBox->addItem("Timestamp",FALSE);
  89. #endif
  90.   n = 0;
  91.   XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
  92.   XtSetArg(args[n],XmNtopWidget, _compress->baseWidget());n++;
  93.   XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
  94.   XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
  95.   XtSetValues(_checkBox->baseWidget(), args, n);
  96.   _checkBox->show();
  97.   _visual = new VkRadioBox("viz",_form);
  98.   _visual->addItem("Full");
  99.   _visual->addItem("Half",TRUE);
  100.   _visual->addItem("Quarter");
  101.   n = 0;
  102.   XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
  103.   XtSetArg(args[n],XmNtopWidget, _checkBox->baseWidget());n++;
  104.   XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
  105.   XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
  106.   XtSetValues(_visual->baseWidget(), args, n);
  107.   _visual->show();
  108.   _startStop = new StartStopPanel("panel", _form);
  109.   _startStop->addCallback(StartStopPanel::actionCallback,
  110.               this,
  111.               (VkCallbackMethod)&SecurityWin::statusChanged);
  112.   n = 0;
  113.   XtSetArg(args[n],XmNtopAttachment, XmATTACH_WIDGET);n++;
  114.   XtSetArg(args[n],XmNtopWidget, _visual->baseWidget());n++;
  115.   XtSetArg(args[n],XmNrightAttachment, XmATTACH_FORM);n++;
  116.   XtSetArg(args[n],XmNleftAttachment, XmATTACH_FORM);n++;
  117.   XtSetValues(_startStop->baseWidget(), args, n);
  118.   _startStop->show();
  119.   _timer = NULL;
  120.   _movie = NULL;
  121.   _monitor = NULL;
  122.   _isBusy = FALSE;
  123.   addView(_form);
  124.   buildMenu();
  125. #ifdef QT
  126.   mvInitQuickTime();
  127. #endif
  128.   VkAddCallbackMethod(VkPrefDialog::prefCallback,
  129.               _pref,
  130.               this,
  131.               &SecurityWin::preference, (void *)NULL);
  132. }
  133.  
  134. void SecurityWin::buildMenu()
  135. {
  136.   _mainMenu = new VkMenuBar((Boolean)TRUE);
  137.   _fileMenu = addMenuPane("File");
  138.   _fileMenu->addAction("Preferences",&SecurityWin::postPrefCallback, (XtPointer)this);
  139. #ifdef NOSHIP
  140.   _fileMenu->addAction("View",&SecurityWin::viewCallback, (XtPointer)this);
  141. #endif
  142.   _fileMenu->addAction("Quit", &SecurityWin::quitCallback, (XtPointer)this);
  143.   _mainMenu->addSubmenu(_fileMenu);
  144.   setMenuBar(_mainMenu);
  145. }
  146.  
  147. void SecurityWin::preference(VkComponent *obj, void *clientData, void *callData)
  148. {
  149.   Pref *pref = (Pref *)obj;
  150.   SecurityWin *win = (SecurityWin *)clientData;
  151.   VkDialogManager::VkDialogReason reason = (VkDialogManager::VkDialogReason)callData;
  152.   if(reason == VkDialogManager::CANCEL) return;
  153.  
  154.   pref->update();
  155. }
  156.  
  157. void SecurityWin::postPrefCallback(Widget, XtPointer clientData, XtPointer)
  158. {
  159.   SecurityWin *obj = (SecurityWin *) clientData;
  160.   obj->postPref();
  161. }
  162.  
  163. #ifdef NOSHIP
  164. void SecurityWin::viewCallback(Widget, XtPointer clientData, XtPointer)
  165. {
  166.   SecurityWin *obj = (SecurityWin *)clientData;
  167.   obj->view();
  168. }
  169.  
  170. void SecurityWin::view()
  171. {
  172.   char *name;
  173.   name = _compress->getFileName();
  174.   if(!mvIsMovieFile(name)){
  175.     fprintf(stderr,"%s is not a movie\n",name);
  176.     return;
  177.   }
  178.   VkSimpleWindow *win = new VkSimpleWindow("viewPlayer");
  179.   MovieController *viewWidget = new MovieController(win->mainWindowWidget(),
  180.                             "view",name,1,FALSE);
  181.   win->setTitle(basename(name));
  182.   viewWidget->show();
  183.   win->show();
  184. }
  185. #endif
  186.  
  187. void SecurityWin::postPref()
  188. {
  189.   _pref->post("");
  190. }
  191.  
  192. void SecurityWin::statusChanged(VkCallbackObject *, void *, void *callData)
  193. {
  194.   char *ptr;
  195.   Action currentAction = (Action) callData;
  196.   switch (currentAction){
  197.   case START:
  198.     if(_timer == NULL){
  199.       buildVideo();
  200.       _timer = new VkPeriodic(_pref->getDelay() * 1000);
  201.       VkAddCallbackMethod(VkPeriodic::timerCallback,
  202.               _timer, this,
  203.               &SecurityWin::grabFrame, (void *)NULL);
  204.       ptr = _compress->getFileName();
  205.       _movie = new NewMovie(ptr, 4.0,
  206.                 _video->getWidth(),
  207.                 _video->getHeight(),
  208.                 _compress->getCurrentCompressor());
  209.       free(ptr);
  210. #ifdef DPS
  211.       _title = new Title(mainWindowWidget(),
  212.              _video->getWidth(),
  213.              _video->getHeight());
  214.       _title->setColor(1.0, 0.0, 0.0);
  215.       _title->setOffset( _video->getWidth()/2, _video->getHeight()/2);
  216. #endif
  217.       if(_checkBox->getValue(1)){
  218.     _monitor = new Monitor(_video->getWidth(), _video->getHeight());
  219.       }
  220.       _sound = 0;
  221.       _timer->start();
  222.     }
  223.     break;
  224.   case STOP:
  225.     if(_timer != NULL){
  226.       _timer->stop();
  227.       VkRemoveCallbackMethod(VkPeriodic::timerCallback,
  228.               _timer, this,
  229.               &SecurityWin::grabFrame, (void *)NULL);
  230. #ifdef DPS
  231.       delete _title;
  232.       _title = NULL;
  233. #endif
  234.       delete _timer;
  235.       _timer = NULL;
  236.       delete _monitor;
  237.       _monitor = NULL;
  238. // reset frame rate in movie based on _sound      
  239.       _movie->setNumAudio(_sound);
  240.       delete _movie;
  241.       _movie = NULL;
  242.       _video->freeBuffer();
  243.       delete [] _RGBFrame;
  244.       delete _video;
  245.       _video = NULL;
  246.       _motion->freeBuffer();
  247.       delete [] _newFrame;
  248.       delete [] _oldFrame;
  249.       delete _motion;
  250.       _motion = NULL;
  251.     }
  252.     break;
  253.   default:
  254.     break;
  255.   }
  256. }
  257.  
  258. void SecurityWin::grabFrame(VkCallbackObject *, void *, void *)
  259. {
  260.   time_t t;
  261.   void *frame;
  262.   Audio *audio;
  263.   int diffs, newSounds;
  264.   if(_isBusy) {
  265.     fprintf(stderr,"Got a spurious timer event\n");
  266.     return;
  267.   }
  268.   _isBusy = TRUE;
  269.   audio = NULL;
  270.   _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _newFrame);
  271.   diffs = diffBuffer((unsigned char *)_oldFrame, (unsigned char *)_newFrame,
  272.              _motion->getSize(),
  273.              _pref->getAccuracy(),
  274.              _pref->getFrames());
  275.   if(diffs){
  276.     _video->loadFrame(_video->getWidth(), _video->getHeight(), _RGBFrame);
  277.     if(_monitor){
  278.       _monitor->add(_RGBFrame);
  279.     }
  280. #ifdef DPS
  281.     if(_checkBox->getValue(3)){
  282.       _title->putImage(_RGBFrame);
  283.       t = time(NULL);
  284.       _title->text(asctime(localtime(&t)),32/getScale());
  285.       frame = _title->getImage();
  286.       if(frame){
  287.     if(!_movie->setFrame(-1,frame)){
  288.       stopOnError();
  289.       return;
  290.     }
  291.     delete [] frame;
  292.       }else{
  293.     if(!_movie->setFrame(-1,_RGBFrame)){
  294.       stopOnError();
  295.       return;
  296.     }
  297.       }
  298.     }else{
  299.       if(!_movie->setFrame(-1,_RGBFrame)){
  300.     stopOnError();
  301.     return;
  302.       }
  303.     }
  304. #else
  305.     if(!_movie->setFrame(-1,_RGBFrame)){
  306.       stopOnError();
  307.       return;
  308.     }
  309. #endif
  310.     if(_checkBox->getValue(2)){
  311.       XForceScreenSaver(XtDisplay(mainWindowWidget()), ScreenSaverReset);
  312.       dprintf("Unblanking screen\n");
  313.     }
  314.     securityApp->busy();
  315.     int busyCount = 1;
  316.     audio = new Audio(); 
  317.     while(busyCount < _pref->getTripCount()){
  318.       frame = _oldFrame; _oldFrame = _newFrame; _newFrame = frame;
  319.       _video->loadFrame(_video->getWidth(), _video->getHeight(), _RGBFrame);
  320.       if(_monitor) _monitor->add(_RGBFrame);
  321.       if(!_movie->setFrame(-1,_RGBFrame)){
  322.     _isBusy = FALSE;
  323.     securityApp->notBusy();
  324.     stopOnError();
  325.     return;
  326.       }
  327.       _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _newFrame);
  328.       diffs = diffBuffer((unsigned char *)_oldFrame, (unsigned char *)_newFrame,
  329.              _motion->getSize(),
  330.              _pref->getAccuracy(),
  331.              _pref->getFrames());
  332.       if(_pref->getTripType() == MOTION){
  333.     if (diffs){
  334.       busyCount = 0;
  335.     }else{
  336.       busyCount++;
  337.     }
  338.       }else{
  339.     busyCount++;
  340.       }
  341.       if(_checkBox->getValue(0)){
  342.     _movie->addAudio(audio);
  343.     _sound = (int)_movie->getNumAudio();
  344.       }else{
  345.     void *foo = audio->getAudio(newSounds);
  346.     _sound += newSounds;
  347.       }
  348.     }
  349.     delete audio;
  350.     securityApp->notBusy();
  351.     _isBusy = FALSE;
  352.   }
  353.   frame = _oldFrame; _oldFrame = _newFrame; _newFrame = frame;
  354.   _isBusy = FALSE;
  355. }
  356.  
  357. #define PIECES 16
  358. #define ROLLCOUNT 8
  359. int SecurityWin::diffBuffer(unsigned char *buf1, unsigned char *buf2, int siz, int pacc, int facc)
  360. {
  361.   int i, out;
  362.   out = 0;
  363.   int innerLoop = siz / PIECES / ROLLCOUNT;
  364.   for(int j = 0; j < PIECES; j++){
  365.     for(i=0; i<innerLoop; i++){
  366.       if(((buf1[1] - buf2[1]) > pacc ) || ((buf2[1] - buf1[1]) > pacc )) out++;
  367.       if(((buf1[3] - buf2[3]) > pacc ) || ((buf2[3] - buf1[3]) > pacc )) out++;
  368.       if(((buf1[5] - buf2[5]) > pacc ) || ((buf2[5] - buf1[5]) > pacc )) out++;
  369.       if(((buf1[7] - buf2[7]) > pacc ) || ((buf2[7] - buf1[7]) > pacc )) out++;
  370.       buf1 += ROLLCOUNT; buf2 += ROLLCOUNT;
  371.     }
  372.     if(out > facc){
  373.       dprintf("The diff on iter %d  = %d \n",j,out);
  374.       return TRUE;
  375.     }
  376.   }
  377.   dprintf("The diff  = %d \n",out);
  378.   return FALSE;
  379. }
  380. #undef ROLLCOUNT
  381. #undef PIECES
  382.  
  383. void SecurityWin::buildVideo()
  384. {
  385.   int scale = getScale();
  386.   _motion = new VideoIn(2,devicenum,FALSE);
  387.   _video = new VideoIn(scale,devicenum);
  388.   if(!_video->hasVideo()){
  389.     fprintf(stderr,"No video input.\n");
  390.     exit(1);
  391.   }
  392.   _RGBFrame = new unsigned char [_video->getSize()];
  393.   _oldFrame = new unsigned char [_motion->getSize()];
  394.   _newFrame = new unsigned char [_motion->getSize()];
  395.   _video->getBuffer();
  396.   _motion->getBuffer();
  397.   _motion->loadFrame(_motion->getWidth(), _motion->getHeight(), _oldFrame);
  398. }
  399.  
  400. int SecurityWin::getScale()
  401. {
  402.   int scale = 1;
  403.   if(_visual->getValue(0) > 0) scale = 1;
  404.   if(_visual->getValue(1) > 0) scale = 2;
  405.   if(_visual->getValue(2) > 0) scale = 4;
  406.   return scale;
  407. }
  408.  
  409. void SecurityWin::stopOnError()
  410. {
  411.   XButtonEvent xev1, xev2;
  412.   Display *dpy;
  413.   Widget w;
  414.   w = _startStop->_stopButton;
  415.   dpy = XtDisplay(w);
  416. // issue a STOP event to StartStop to end the
  417. // record session
  418.   xev1.type = ButtonPress;
  419.   xev2.type = ButtonRelease;
  420.   xev1.send_event = xev2.send_event = TRUE;
  421.   xev1.display = xev2.display = dpy;
  422.   xev1.window = xev2.window = XtWindow(w);
  423.   xev1.x = xev1.y = xev2.x = xev2.y = 0;
  424.   xev1.x_root = xev1.y_root = xev2.x_root = xev2.y_root = 0;
  425.   xev1.state = 0;
  426.   xev2.state = 1;
  427.   xev1.button = xev2.button = Button1;
  428.   xev1.same_screen = xev2.same_screen = TRUE;
  429.   XtDispatchEvent( (XEvent *)&xev1);
  430.   XtDispatchEvent( (XEvent *)&xev2);
  431.   XFlush(dpy);
  432. }
  433.